<?php
session_start();
unset($_SESSION['rand_code']);
function generateCode($characters) {
      /* list all possible characters, similar looking characters and vowels have been removed */
include_once($_SERVER['DOCUMENT_ROOT'].'/resources/lib/database.php');
$data=new database();
$data->db(DATABASE)->collection('captcha');
$data->criteria(array('rand'=>intval(rand(1,20191))));
$row=$data->findOne();
$data->close();



return ucfirst(trim($row['word']));
/*
      $possible = '23456789bcdfghjkmnpqrstvwxyz';
      $code = '';
      $i = 0;
      while ($i < $characters) {
         $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
         $i++;
      }
      return $code;
*/
}


if (empty($_SESSION['rand_code'])) {
        $_SESSION['rand_code']=generateCode(9);
}

$imgX = 108;
$imgY = 27;
$width=$imgX;
$height=$imgY;
$image = imagecreatetruecolor($imgX, $imgY);
$line1randX = mt_rand(0,55);
$line1randY = mt_rand(0,25);
//$backgr_col = imagecolorallocate($image, 82,92,99);
//$border_col = imagecolorallocate($image, 102,102,102);
$backgr_col = imagecolorallocate($image, 255,255,255);
$border_col = imagecolorallocate($image, 255,255,255);


$text_col = imagecolorallocate($image, 71,132,224);
$text_co = imagecolorallocate($image, 147,181,237);

imagefilledrectangle($image, 0, 0, 200, 41, $backgr_col);
imagerectangle($image, 0, 0, 199, 40, $border_col);

$font = "./DejaVuSansCondensed.ttf"; // it's a Bitstream font check www.gnome.org for more
$font = "./Base05.ttf"; // it's a Bitstream font check www.gnome.org for more
$font	= './VeraSansBold.ttf';
$font_size = 14;
$angle = rand(-5,6);
$box = imagettfbbox($font_size, $angle, $font, $_SESSION['rand_code']);
$x = (int)($imgX - $box[4]) / 2;
$y = (int)($imgY - $box[5]) / 2;

imagettftext($image, $font_size, $angle, $x, $y, $text_co, $font, $_SESSION['rand_code']);
imagettftext($image, $font_size, $angle, ($x-4), $y, $text_col, $font, $_SESSION['rand_code']);
/*
for($i=0;$i<10;$i++){
      //  $angle = rand(0,10);   // cheap and easy but might be nicer to use tan
        $y= sin($i);   // remember sin waves?
        imagettftext($image,$font_size,$angle,$i,$y, $text_co, $font,substr($_SESSION['rand_code'],$i-1,1));  //write one char at a time
} 
*/
imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR);
//imageline($image,5,8,$line1randX,$line1randY,$color1);
//imageline($image,15,15,40,15,$color1);

header("Content-type: image/jpeg");
//imagepng($image,NULL,9);
imagejpeg($image,NULL,90);
imagedestroy ($image);
?>
